#!/bin/sh
### BEGIN INIT INFO
# Provides:          sergate
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: ServerStatus Master
# Description:       ServerStatus Master Server
### END INIT INFO

. /lib/lsb/init-functions

# Change this according to your setup!
DAEMON_PATH="/usr/local/share/ServerStatus/server"
WEB_PATH="/var/www/botox.bz/status"
DAEMON="sergate"
OPTS="-d $WEB_PATH"
RUNAS="www-data"

DESC="ServerStatus Master"

PIDFILE=/var/run/sergate.pid

test -x "$DAEMON_PATH/$DAEMON" || exit 5

case $1 in
	start)
		log_daemon_msg "Starting $DESC"
		start-stop-daemon --start --background --pidfile "$PIDFILE" --make-pidfile --chuid "$RUNAS" -d "$DAEMON_PATH" --exec "./$DAEMON" -- $OPTS
		if [ $? -ne 0 ]; then
			log_end_msg 1
		else
			log_end_msg 0
		fi
		;;
	stop)
		log_daemon_msg "Stopping $DESC"
		start-stop-daemon --stop --pidfile "$PIDFILE" --retry 5

		if [ $? -ne 0 ]; then
			log_end_msg 1
		else
			log_end_msg 0
		fi
		;;
	restart)
		$0 stop
		sleep 1
		$0 start
		;;
	status)
		status_of_proc -p "$PIDFILE" "$DAEMON" "sergate" && exit 0 || exit $?
		;;
	reload)
		log_daemon_msg "Reloading $DESC"
		start-stop-daemon --stop --signal HUP --pidfile "$PIDFILE"

		if [ $? -ne 0 ]; then
			log_end_msg 1
		else
			log_end_msg 0
		fi
		;;
	*)
		echo "Usage: $0 {start|stop|restart|reload|status}"
		exit 2
		;;
esac
